package com.xinhe.web.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.BusinessException;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.*;
import com.xinhe.web.reposity.AddressRepository;
import com.xinhe.web.reposity.OrderAddressRepository;
import com.xinhe.web.request.OrderAddressModel;
import com.xinhe.web.session.UserSessionManager;
import com.xinhe.web.util.Snowflake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;

@Service

public class OrderAddressService {
    @Autowired
    OrderAddressRepository orderAddressRepository;
    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    UserSessionManager userSessionManager;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }
    public OrderAddress findByOrderIdAndType(Long orderId, String type) {
        OrderAddress orderAddress = this.orderAddressRepository.findByOrderIdAndType(orderId, type);
        return orderAddress;
    }
    @Transactional
    public Result saveOrderAddress(Long orderId, Long addressId,String type) {
        Address address = this.addressRepository.findByAddress(addressId);
        if (address == null) {
            return Result.failed(104,"地址不存在");
        }
        OrderAddress orderAddress = new OrderAddress();
        orderAddress.setName(address.getName());
        orderAddress.setTelephone(address.getTelephone());
        orderAddress.setProvince(address.getProvince());
        orderAddress.setCity(address.getCity());
        orderAddress.setArea(address.getArea());
        orderAddress.setDetail(address.getDetail());
        orderAddress.setOrderId(orderId);
        orderAddress.setUserId(this.userSessionManager.getUserId());
        orderAddress.setType(type);
        this.orderAddressRepository.save(orderAddress);
        return Result.ok();
    }

    public QueryResults<OrderAddress> pageOrderAddress(int pageNum, int pageSize) {
        QOrderAddress qOrderAddress = QOrderAddress.orderAddress;
        return queryFactory.select(
                        Projections.bean(
                                OrderAddress.class,
                                qOrderAddress.orderId,
                                qOrderAddress.id,
                                qOrderAddress.area,
                                qOrderAddress.city,
                                qOrderAddress.name,
                                qOrderAddress.detail,
                                qOrderAddress.province,
                                qOrderAddress.telephone,
                                qOrderAddress.type,
                                qOrderAddress.userId
                        )
                ).from(qOrderAddress)
                .offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }


    public Result saveModel(OrderAddressModel model) {
        OrderAddress orderAddress = new OrderAddress();
        orderAddress.setId(Snowflake.generateId());
        BeanUtils.copyProperties(model, orderAddress);
        return Result.ok();
    }

    public Result findById(Long orderAddressId) {
        return Result.ok(orderAddressRepository.findById(orderAddressId).get());
    }

    public Result delete(Long orderAddressId) {
        orderAddressRepository.deleteById(orderAddressId);
        return Result.ok();
    }
}
